import 'dart:io';

import 'package:baidu_map/pages/single_location.dart';
import 'package:baidu_map/pages/single_location_without_map.dart';
import 'package:baidu_map/widgets/function_item.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmflocation/flutter_bmflocation.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'
    show BMFMapSDK, BMF_COORD_TYPE;
import 'pages/series_location.dart';
import 'utils/log_util.dart';


final List<Permission> needPermissionList = [
  Permission.location,
];

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  void initState() {
    super.initState();


    _checkPermissions();
    
  }

  initBaiduApi() {
    LogUtil.d("初始化咯~~~~");
    LocationFlutterPlugin myLocPlugin = LocationFlutterPlugin();
    myLocPlugin.setAgreePrivacy(true);
    BMFMapSDK.setAgreePrivacy(true);
    if (Platform.isIOS) {
      /// 设置ios端ak, android端ak可以直接在清单文件中配置
      myLocPlugin.authAK('yFDD3IlfSk2xTdQ0G41MjRnzd5gUVG1C');
      BMFMapSDK.setApiKeyAndCoordType(
          'yFDD3IlfSk2xTdQ0G41MjRnzd5gUVG1C', BMF_COORD_TYPE.BD09LL);
    } else if (Platform.isAndroid) {
      // Android 目前不支持接口设置Apikey,
      // 请在主工程的Manifest文件里设置，详细配置方法请参考官网(https://lbsyun.baidu.com/)demo
      BMFMapSDK.setCoordType(BMF_COORD_TYPE.BD09LL);
    }

    /// iOS端鉴权结果
    myLocPlugin.getApiKeyCallback(callback: (String result) {
      String str = result;
      debugPrint('鉴权结果: $str');
    });
  }

  @override
  void reassemble() {
    super.reassemble();
    _checkPermissions();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _checkPermissions() async {
    Map<Permission, PermissionStatus> statuses =
      await needPermissionList.request();
    LogUtil.d(statuses);

    statuses.forEach((key, value) {
      debugPrint('$key permissionStatus is $value');
    });
    initBaiduApi();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: const FlutterBMFLocationDemo()
    );
  }
}

class FlutterBMFLocationDemo extends StatelessWidget {
  const FlutterBMFLocationDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(children: const <Widget>[
      FunctionItem(
        label: '单次定位(无地图)',
        subLabel: 'singleLocationWithoutMapPage',
        target: SingleLocationWithoutMapPage()
      ),
      FunctionItem(
        label: '单次定位',
        subLabel: 'singleLocationPage',
        target: SingleLocationPage()
      ),
      FunctionItem(
          label: '连续定位',
          subLabel: 'seriesLocationPage',
          target: SeriesLocationPage()
      ),
      // FunctionItem(
      //     label: '圆形地理围栏',
      //     subLabel: 'circleGeofencePage',
      //     target: CircleGeofencePage()
      // ),
      // FunctionItem(
      //     label: '多边形地理围栏',
      //     subLabel: 'polygonGeofencePage',
      //     target: PolygonGeofencePage()
      // ),
      // FunctionItem(
      //     label: '监听设备朝向',
      //     subLabel: 'headingLocationPage',
      //     target: HeadingLocationPage()
      // ),
      MyItem()
    ],);
  }
}

class MyItem extends StatelessWidget {
  const MyItem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ListTile(
          title: const Text('输出看看'),
          subtitle: const Text('测试输出'),
          trailing: const Icon(Icons.keyboard_arrow_right),
          onTap: () async {
            await showDatePicker(
              context: context,
              initialDate: DateTime.now(),
              firstDate: DateTime(2015, 8),
              lastDate: DateTime(2101),
            );
            // LogUtil.v('verbose message');
            // LogUtil.d('debug message');
            // LogUtil.i('info message');
            // LogUtil.w('warning message');
            // LogUtil.e('error message');
            // LogUtil.wtf('wft message');
          },
        ),
        const Divider(height: 1, indent: 16,)
      ],
    );
  }
}

